package com.yanqu.road.dao.impl.activity.dunhuangshiku;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuNpcBloodData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/9/13 18:10
 */
public class DunHuangShiKuNpcBloodDataDaoImpl extends TempDataObjectDao<DunHuangShiKuNpcBloodData> {


    public static String getDunHuangShiKuNpcBloodDataSql = "select * from `t_c_dunhuangshiku_npc_blood_data` where `activityId`=? and groupId = ?";
    public Map<Integer, DunHuangShiKuNpcBloodData> getDunHuangShiKuNpcBloodDataMap(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DunHuangShiKuNpcBloodData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getDunHuangShiKuNpcBloodDataSql);
            int i = 1;
            call.setLong(i++, activityId);
            call.setInt(i, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DunHuangShiKuNpcBloodData npcBloodData = parse(rs);
                map.put(npcBloodData.getEventId(), npcBloodData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


    public static String updateDunHuangShiKuNpcBloodDataSql = "update `t_c_dunhuangshiku_npc_blood_data` set blood=?,createTime=? where activityId=? and eventId=? and groupId=?";

    @Override
    protected boolean updateData(DunHuangShiKuNpcBloodData dunHuangShiKuNpcBloodData) {
        dunHuangShiKuNpcBloodData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuNpcBloodData.getBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuNpcBloodData.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuNpcBloodData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuNpcBloodData.getEventId()));
        para.put(i, new DbParameter(Types.INTEGER, dunHuangShiKuNpcBloodData.getGroupId()));
        if (getDb().executeNoneQuery(updateDunHuangShiKuNpcBloodDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDunHuangShiKuNpcBloodDataSql = "insert into `t_c_dunhuangshiku_npc_blood_data`(activityId,eventId,blood,createTime, groupId) values (?,?,?,?,?)";

    @Override
    protected boolean addData(DunHuangShiKuNpcBloodData dunHuangShiKuNpcBloodData) {
        dunHuangShiKuNpcBloodData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuNpcBloodData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuNpcBloodData.getEventId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuNpcBloodData.getBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuNpcBloodData.getCreateTime()));
        para.put(i, new DbParameter(Types.INTEGER, dunHuangShiKuNpcBloodData.getGroupId()));
        if (getDb().executeNoneQuery(insertDunHuangShiKuNpcBloodDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DunHuangShiKuNpcBloodData parseData(ResultSet rs) throws SQLException {
        DunHuangShiKuNpcBloodData dunHuangShiKuNpcBloodData = new DunHuangShiKuNpcBloodData();
        dunHuangShiKuNpcBloodData.setActivityId(rs.getLong("activityId"));
        dunHuangShiKuNpcBloodData.setEventId(rs.getInt("eventId"));
        dunHuangShiKuNpcBloodData.setBlood(rs.getLong("blood"));
        dunHuangShiKuNpcBloodData.setCreateTime(rs.getLong("createTime"));
        dunHuangShiKuNpcBloodData.setGroupId(rs.getInt("groupId"));

        return dunHuangShiKuNpcBloodData;
    }

}
